package com.zfy.servlet;

import com.zfy.entity.Product;
import com.zfy.service.ProductService;
import com.zfy.utils.BaseServlet;
import com.zfy.utils.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {

    ProductService productService = new ProductService();

    public void getProducts(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer limit = Integer.parseInt(req.getParameter("limit"));
        String pname = req.getParameter("pname");
        String min = req.getParameter("min");
        String max = req.getParameter("max");
        String cid = req.getParameter("cid");
        Double minPrice = null,maxPrice = null;
        Integer cateId = null;

        if(min != null && !min.equals("")){
            minPrice = Double.parseDouble(min);
        }
        if( max != null && !max.equals("")){
            maxPrice = Double.parseDouble(max);
        }
        if( cid != null && !cid.equals("")){
            cateId = Integer.parseInt(cid);
        }
        List<Product> ProductList = productService.getProduct(page,limit,pname,minPrice,maxPrice,cateId);
        List<Product> all = productService.getProduct(null,null,pname,minPrice,maxPrice,cateId);
        if(ProductList !=null && ProductList.size() > 0){
            out.println(Result.toClient(0,"",all.size(),ProductList));
        }else{
            out.println(Result.toClient(1,"无数据"));
        }
        out.flush();
        out.close();
    }
    public void addProducts(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
    }
}
